<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />

    <!-- obj初始值{}, 所以obj.title是undefined (证明网络请求中的数据还没回来) -->
    <div style="text-align: center"  v-if="obj.title === undefined" >

    <van-loading class="loading" color="#1989fa"/>
    </div>
    <!-- 文章详情 + 文章评论列表区域 (网络请求没回来之前,先让van-loading在原地转圈展示) -->
    <div v-else>
      <!-- 文章信息区域 -->
      <div class="article-container">
        <!-- 文章标题 -->
        <h1 class="art-title">{{ obj.title }}</h1>

        <!-- 用户信息 -->
        <van-cell center :title="obj.aut_name" :label="formatTime(obj.pubdate)">
          <template #icon>
            <img :src="obj.aut_photo" alt="" class="avatar" />
          </template>
          <template #default>
            <div>
              <van-button
                v-if="obj.is_followed"
                @click="followFn(true)"
                type="info"
                size="mini"
                >已关注</van-button
              >
              <van-button
                v-else
                @click="followFn(false)"
                icon="plus"
                type="info"
                size="mini"
                plain
                >关注</van-button
              >
            </div>
          </template>
        </van-cell>

        <!-- 分割线 -->
        <van-divider></van-divider>

        <!-- 文章内容 -->
        <div class="art-content" v-html="obj.content"></div>

        <!-- 分割线 -->
        <van-divider>End</van-divider>

        <!-- 点赞 -->
        <div class="like-box">
          <van-button
            v-if="obj.attitude"
            @click="attitudeFn(true)"
            icon="good-job"
            type="danger"
            size="small"
            >已点赞</van-button
          >
          <van-button
            v-else
            @click="attitudeFn(false)"
            icon="good-job-o"
            type="danger"
            plain
            size="small"
            >点赞</van-button
          >
        </div>
      </div>

      <!-- 文章评论区域 -->
      <CommentList
        :arr="commentList"
        @like="likeFn"
        @sendFn="sendFn"
        @more="moreFn"
        ref="com"
        :allCount="allCommentCount"
      ></CommentList>
    </div>

  </div>
</template>

<script>
// 如果axios发现参数的值为null, 则自动忽略此键值对
import {
  articleDetailAPI,
  authorFollowingAPI,
  authorUnFollowingAPI,
  articleAttitudeAPI,
  articleUnAttitudeAPI,
  commentListAPI,
  commentLikeAPI,
  commentDisLikeAPI,
  UnshiftComment
} from '@/api'
import { formatTime } from '@/utils/date.js'
import CommentList from './components/CommentList.vue'
export default {
  components: {
    CommentList
  },
  data () {
    return {
      obj: {}, // 文章对象
      commentList: [], // 文章评论列表
      offset: null, // 评论列表-分页 (偏移量)
      allCommentCount: 0 // 评论总数量
    }
  },
  async created () {
    // 文章详情
    const [, res] = await articleDetailAPI({
      artId: this.$route.query.art_id
    })
    // console.log(res)
    this.obj = res.data.data
    // 手动调用评论数据
    this.getCommentList()
  },
  methods: {
    async getCommentList () {
      // 文章评论列表
      const [, commentRes] = await commentListAPI({
        artId: this.$route.query.art_id,
        offset: this.offset
      })
      // console.log(commentRes)
      if (commentRes.data.data.results.length === 0) {
        this.$refs.com.finished = true
      } else { // 当前本次请求有数据
        // 把偏移id 保存起来 - 为了下次再调用getCommentList做偏移量分页
        this.offset = commentRes.data.data.last_id
      }
      this.allCommentCount = commentRes.data.data.total_count
      // 合并新数组
      this.commentList = [...this.commentList, ...commentRes.data.data.results]
      this.$refs.com.loading = false
    },
    formatTime: formatTime,
    async followFn (bool) { // 关注
      if (bool === true) { // 用户点击的是 已关注按钮
        const [err] = await authorFollowingAPI({
          autId: this.obj.aut_id
        })
        // 没有错误再改变数据影响页面状态
        if (!err) this.obj.is_followed = false
      } else { // 用户点击的是 关注按钮
        const [err] = await authorUnFollowingAPI({
          autId: this.obj.aut_id
        })
        if (!err) this.obj.is_followed = true
      }
    },
    async attitudeFn (bool) { // 点赞
      if (bool === true) { // 用户点击的是 已点赞按钮
        const [err] = await articleAttitudeAPI({
          artId: this.obj.art_id
        })
        // 没有错误再改变数据影响页面状态
        if (!err) this.obj.attitude = false
      } else { // 用户点击的是 点赞按钮
        const [err] = await articleUnAttitudeAPI({
          artId: this.obj.art_id
        })
        if (!err) this.obj.attitude = true
      }
    },
    async likeFn (bool, commentObj) { // 点小爱心
      if (bool === true) { // 表示要取消爱心
        const [err] = await commentDisLikeAPI({
          commentId: commentObj.com_id.toString()
        })
        // console.log(err)
        if (!err) commentObj.is_liking = false
      } else { // 表示要点亮爱心
        const [err] = await commentLikeAPI({
          commentId: commentObj.com_id.toString()
        })
        if (!err) commentObj.is_liking = true
      }
    },
    async sendFn (commentText) { // 点击发布评论
      const [, res] = await UnshiftComment({
        artId: this.$route.query.art_id,
        commentText: commentText
      })
      // console.log(res)
      this.commentList.unshift(res.data.data.new_obj)
    },
    // 加载更多评论列表
    moreFn () {
      this.getCommentList()
    }
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}

.loading {
  margin-top: 50px;
}
</style>
